<template>
  <div class="layout-all">
    <control :config="config" />
    <div class="layout-header" style="float:left">
      <router-link to="./form/">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin-left:0px"
        >添加</el-button>
      </router-link>
    </div>
    <div class="layout-content">
      <el-table
        :data="tableData"
        stripe
        align="center"
        highlight-current-row

        height="800"
        style="width: 100%;margin-top:50px"
      >
        <el-table-column align="center" prop="id" label="会员编号" width="80" :fixed="isMobile()?'left':undefined"/>
        <el-table-column align="center" prop="account" label="账号" width="150" />
        <el-table-column align="center" prop="level_id" label="会员等级" width="100" >
          <template slot-scope="scope">{{level[scope.row.level_id-1]+(scope.row.proxy_id>1?'('+proxy[scope.row.proxy_id-1]+')':'')}}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="nickname" label="昵称" minWidth="150">
           <template slot-scope="scope">
             
             <div icon="el-icon-platform-eleme" :style="{color:scope.row.sex==1?'#409eff':scope.row.sex==0?'#a4959a':'#D650BD'}" >
               {{scope.row.nickname}}</div>
            </template>
        </el-table-column>
        <el-table-column align="center" prop="parent_id" label="上级推荐人" width="150" >
          <template slot-scope="scope" v-if="scope.row.parent_user!=undefined">
             <div v-if="scope.row.parent_user!=undefined" icon="el-icon-platform-eleme" :style="{color:scope.row.parent_user.sex==1?'#409eff':scope.row.parent_user.sex==0?'#a4959a':'#D650BD'}" >
               {{scope.row.parent_user.nickname}}
               </div>
            </template>
        </el-table-column>
        <el-table-column align="center" prop="headimgurl" label="头像" width="150">
          <template slot-scope="scope">
            <el-popover
            placement="right"
            trigger="hover">
            <img :src="scope.row.headimgurl" class="imgStyle" style="max-height: 300px;max-width: 750px"/>
            <img slot="reference" :src="scope.row.headimgurl" style="max-height: 50px;max-width: 150px">
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="status" label="是否启用" width="100" v-if="model=='admin'">
          <template slot-scope="scope">
            <el-button
              :type="scope.row.status === 1 ? 'primary' : 'success'"
              size="mini"
              @click="change_status(scope.row.id,'status' ,scope.row.status)"
            >{{ scope.row.status === 1 ? "是" : "否" }}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="balance" label="账户余额" width="100" />
        <el-table-column align="center" prop="integral" :label="integral_name+'余额'" width="100" />
        <el-table-column align="center" prop="create_time" label="注册日期" width="110" />

        <el-table-column
          align="center"
          prop="telephone"
          :fixed="isMobile()?undefined:'right'"
          :width="model=='admin'?'550':'300'"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              v-if="model=='admin'"
              @click="message_click(scope.row)"
            >消息</el-button>
            <el-button
              type="primary"
              size="mini"
              @click="money_click(scope.row)"
            >资金</el-button>
            <el-button
              type="primary"
              size="mini"
              @click="order_click(scope.row)"
            >订单</el-button>
            <el-button
              type="primary"
              size="mini"
              @click="commission_click(scope.row)"
            >分佣</el-button>
             <el-button
              type="primary"
              size="mini"
              @click="child_click(scope.row)"
            >团队</el-button>
            <el-button
              type="success"
              size="mini"
              icon="el-icon-s-tools"
              v-if="model=='admin'"
              @click="stock_click(scope.row)"
            />
            <router-link :to="'./form/' + scope.row.id" v-if="model=='admin'">
            <el-button size="mini" type="primary"  icon="el-icon-edit" />
            </router-link>
            <el-button
              type="danger"
              size="mini"
              v-if="scope.row.id>1&&model=='admin'"
              icon="el-icon-delete"
              @click="onDel(scope.row.id)"
            />
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      :current-page="current_page"
      :page-size="per_page"
      layout="total, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog title="修改密码" :visible.sync="dialog_visible" :width="isMobile()?'100%':'30%'">
       <el-tabs type="card" @tab-click="handleClick">
        <el-tab-pane label="修改登录密码">
          <el-form
            ref="stock_form"
            :model="stock_form"
            label-width="100px"
          >
            <el-form-item prop="pwd" label="登录密码:" >
              <el-input
                v-model="stock_form.pwd"
                placeholder="请输入登录密码"
              />
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="修改支付密码">
           <el-form
            ref="stock_form"
            :model="stock_form"
            label-width="100px"
          >
            <el-form-item prop="pay_pwd" label="支付密码:" >
              <el-input
                v-model="stock_form.pay_pwd"
                placeholder="请输入支付密码"
              />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
       <span slot="footer" class="dialog-footer">
            <el-button @click="dialog_visible = false">取 消</el-button>
            <el-button type="primary" @click="modify_stock()"
              >确 定</el-button
            >
          </span>
    </el-dialog>
    <drawer title="消息记录" :isShow="message_visible"  @update:isShow="message_visible=false" width=".8">
       <messageLog v-if="message_visible" :user_id="user_id" :nickname="nickname"/>
      </drawer>
    <drawer title="订单记录" :isShow="order_visible"  @update:isShow="order_visible=false" width=".8">
       <orderList v-if="order_visible" :user_id="user_id" :model="model"/>
    </drawer>
    <drawer title="下级成员" :isShow="child_visible"  @update:isShow="child_visible=false" width=".5">
      <team-list :user_id="user_id"/>
    </drawer>
    <drawer :title="'资金变动<'+nickname+'>'" :isShow="money_visible"  @update:isShow="money_visible=false" width=".6">
      <account-money-log :user_id="user_id"/>
    </drawer>
    <drawer title="分佣记录" :isShow="commission_visible"  @update:isShow="commission_visible=false" width=".8">
      <commission-log  :user_id="user_id"/>
    </drawer>
  </div>
</template>

<script>
import control from '../../components/control/control'
import orderList from '../../components/orderlist/orderList'
import commissionLog from '../../components/orderlist/commissionLog'
import accountMoneyLog from '../../components/orderlist/accountMoneyLog'
import messageLog from '../../components/orderlist/messageLog'
import TeamList from '../../components/orderlist/TeamList'
export default {
  name:'user_index',
  components: { control,orderList ,commissionLog,accountMoneyLog,messageLog,TeamList},
  data() {
    return {
      changePassType:0,
      spec_loading: false,
      message_visible: false,
      money_visible: false,
      spec_dialog: false,
      dialog_visible: false,
      order_visible: false,
      //下级管理
      child_visible:false,
      money_data:[],
      stock_form:{
        password:'',
        pay_password:'',
      },
      config: [
        { type: 'text', placeholder: '请输入用户名或编号', field: 'title', val: '' },
        {
          type: 'select',
          placeholder: '请选择等级',
          field: 'level_id',
          val: '',
          options: [
            { value: '', label: '全部' },
          ]
        },
      ],
      dateTime: '',
      nickname: '',
      value: '',
      searchWhere: {
        title: ''
      },
      tableData: [],
      level: [],
      proxy: [],
      total: 0,
      team_amount: 0,
      team_num: 0,
      current_page: 1,
      per_page: 14,
      integral_name: '积分',
      user_id:0,
      id:'',
      
      commission_visible:false,

    }
  },
  computed:{
    model(){
      return this.$store.state.user.model
    }
  },
  activated(){
    if(this.$getUpdate()){
      this.onSearch()
    }
  },
  created() {
    this.onSearch()
  },
  methods: {
    //修改密码
    modify_stock () {
      var form = {}
      if (this.changePassType == 0) {
        //修改登录密码
        form.pwd = this.stock_form.pwd
      } else if (this.changePassType == 1) {
        //修改支付密码
        form.pay_pwd = this.stock_form.pay_pwd
      }
      form.id = this.user_id
      this.get('/admin/User/save', form, 'POST').then(e => {
         this.$message({
          message: '修改成功',
          type: 'success'
        });
        // this.dialog_visible = false;
        this.onSearch()
      })
    },
    handleClick (item) {
      this.changePassType = item.index
    },
    stock_click(row) {
        this.user_id = row.id
        console.log(this.user_id)
        this.dialog_visible = true;
    },
    //资金管理
    money_click(row) {
       this.nickname=row.nickname
       this.user_id = row.id
       console.log(this.user_id)
      this.money_visible = true
    },
    message_click(row) {
       this.user_id = row.id
       console.log(this.user_id)
       this.nickname=row.nickname
      this.message_visible = true
    },

    //订单记录
    order_click(row) {
        this.user_id = row.id
        console.log(this.user_id)
        this.order_visible = true;
    },
    //分佣查询
    commission_click (row) {
      this.user_id = row.id
      console.log(this.user_id)
      this.commission_visible = true
    },
    child_click(row){
      this.user_id=row.id
       this.child_visible = true
    },
    // 用户查询
    handleSizeChange(val) {
      this.onSearch()
    },
    handleCurrentChange(val) {
      this.current_page = val
      this.onSearch()
    },
    onSearch() {
      const form = {}
      form.page = this.current_page
      this.current_page = 1
      form.map = this.searchWhere
      this.get(this.model+'/User/index', form, 'POST').then(e => {
        this.tableData = e.list.data
        this.per_page = e.list.per_page
        this.total = e.list.total
        this.level = e.level
        this.proxy = e.proxy
        
        this.current_page = e.list.current_page
        this.integral_name = e.integral_name
        this.config[1].options=[{ value: '', label: '全部' }]
        for(let i in this.level){
          let tmp={ value: Number(i)+1, label: this.level[i] }
          this.config[1].options.push(tmp)

        }

      })
    },
    // 添加
    onAdd() {},
    // 编辑
    onEdit(id) {
      let mapList={map:this.searchWhere,current_page:this.current_page}
      localStorage.setItem(process.env.CACHE_CONTROL,JSON.stringify(mapList))
      this.$router.push('./form?id='+id);
    },
    // 修改状态
    change_status(id, field, val) {
      this.update(id, val, field, 'User').then(e => {
        e ? this.onSearch() : ''
      })
    },
    // 删除
    onDel(id) {
      this.$msg_confirm().then(e => {
        if (e) {
          this.get('/admin/User/del?id=' + id, this.form, 'POST').then(e => {
            this.onSearch()
          })
        }
      })
    }
  }
}
</script>

<style>

.el-tag--light {
  margin-left:20px;
}

.flex_row {
  display: flex;
  align-items: center;
}
.flex_start {
  justify-content: flex-start;
}
.fyye {
  margin-left:10px;
}
.layout-all {
  margin-top: 20px;
}
.layout-content {
  margin-top: 20px;
}
</style>>
